package ffmvc.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.gson.Gson;
import ffmvc.model.AggManagerPerformance;
import ffmvc.model.AggPerformance;
import ffmvc.model.AggTeamPerformance;
import ffmvc.model.JdbcDataAccess;
import ffmvc.model.Performance;
import ffmvc.model.PerformancesAll;
import ffmvc.model.Player;
import ffmvc.model.Team;
import ffmvc.model.TeamPerformancesAll;

@Controller
public class HelloController {

	@Autowired
	private JdbcDataAccess dataAccess;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String printWelcome(ModelMap model, HttpServletRequest request) {
		return "index";
	}

	@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public @ResponseBody
	List<AggManagerPerformance> returnPerformances(ModelMap model, HttpServletRequest request) {
		return dataAccess.getTopManagerPerformancesInPeriod(10, 20, 2013);
	}

	@RequestMapping(value = "/players", method = RequestMethod.GET)
	public @ResponseBody
	List<AggPerformance> returnPlayersPerformances(ModelMap model, HttpServletRequest request) {
		return dataAccess.getTopPerfomancesInPeriod(2, 7, "2013", 20);
	}

	@RequestMapping(value = "/player/{id}", method = RequestMethod.GET)
	public String returnPlayerPerformances(@PathVariable("id") String id, ModelMap model) {

		List<AggPerformance> homePerformances = dataAccess.getPlayerPerfomancesHome(id, "2013");
		List<AggPerformance> awayPerformances = dataAccess.getPlayerPerfomancesAway(id, "2013");
		List<Performance> performances = dataAccess.getAllPerformancesByPlayer(id);
		Player player = dataAccess.getPlayerById(id);
		Team team = dataAccess.getTeamById(player.getTeamId());
		Gson gson = new Gson();

		model.addAttribute("performances", gson.toJson(performances));
		model.addAttribute("homePerformances", gson.toJson(homePerformances));
		model.addAttribute("awayPerformances", gson.toJson(awayPerformances));
		model.addAttribute("player", gson.toJson(player));
		model.addAttribute("team", team);

		return "player";

	}

	@RequestMapping(value = "/team/{id}", method = RequestMethod.GET)
	public String returnTeamPerformances(@PathVariable("id") String id, ModelMap model) {

		List<AggPerformance> homePerformances = null;
		List<AggPerformance> awayPerformances = null;
		List<AggPerformance> overallPerformances = null;
		PerformancesAll performances = dataAccess.getTeamPerfomances(new PerformancesAll(), id, "2013");
		Team team = dataAccess.getTeamById(id);
		Gson gson = new Gson();

		homePerformances = performances.getHome();
		awayPerformances = performances.getAway();
		overallPerformances = performances.getOverall();

		model.addAttribute("performances", gson.toJson(overallPerformances));
		model.addAttribute("homePerformances", gson.toJson(homePerformances));
		model.addAttribute("awayPerformances", gson.toJson(awayPerformances));
		model.addAttribute("team", team);
		return "team";

	}

	@RequestMapping(value = "/teams", method = RequestMethod.GET)
	public String returnTeamsPerformances(ModelMap model, HttpServletRequest request) {

		List<AggTeamPerformance> homePerformances = null;
		List<AggTeamPerformance> awayPerformances = null;
		List<AggTeamPerformance> overallPerformances = null;
		TeamPerformancesAll performances = dataAccess.getTeamsPerfomances(new TeamPerformancesAll(), "2013");
		Gson gson = new Gson();

		homePerformances = performances.getHome();
		awayPerformances = performances.getAway();
		overallPerformances = performances.getOverall();

		model.addAttribute("performances", gson.toJson(overallPerformances));
		model.addAttribute("homePerformances", gson.toJson(homePerformances));
		model.addAttribute("awayPerformances", gson.toJson(awayPerformances));
		return "teams";
	}

}
